import { ref } from "vue"
import axios from "axios"
import { addInfo } from './page'
import { defineStore } from "pinia"

export const allGroupMembers = ref([])

export function getAllGroupMember(){
    axios({
        method : 'get',
        url : 'https://47.120.69.214:8787/group/getAllUser',
        params : {
            "groupId" : addInfo.value.id
        }
    })
    .then(response => {
        allGroupMembers.value = response.data
    })
    .catch(error => {
        console.log(error)
    })
}

//获取群聊中的用户,并标注当前用户与他们的关系
export const useGetGroupMembers = defineStore('groupMembers', () => {
    const groupMembers = ref([])
    function get(groundId, userId){
    axios({
        method : 'get',
        url : 'https://47.120.69.214:8787/group/getAllUserWithRelation',
        params : {
            "groupId" : groundId,
            "userId" : userId
        }
    })
    .then(response => {
        // console.log(response.data)
        groupMembers.value = response.data
    })
    .catch(error => console.log(error))
}
    return {
        groupMembers,
        get
    }}
)

//退出群聊
export function exitGroup(userId, groupId){
    sessionStorage.removeItem('friend')
    axios({
        menthod: 'get',
        url : 'https://47.120.69.214:8787/group/exitGroup',
        params:{
            "groupId" : groupId,
            "userId" : userId
            }
    })
    .catch( error => console.log(error))
}

//删除群聊
export function deleteGroup(groupId){
    sessionStorage.removeItem('friend')
    axios({
        menthod: 'get',
        url : 'https://47.120.69.214:8787/group/delete',
        params:{
            "groupId" : groupId,
            }
    })
    .catch( error => console.log(error))
}